//
//  GoodsMingXiViewController.m
//  Experss
//
//  Created by wmwMAC on 17/8/10.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "GoodsMingXiViewController.h"

#import "DingDanMingXiTableViewCell.h"

#import "TiJiaoDingDanModle.h"
#import "TiJiaoDingDanSkuModle.h"
#import "DingDanSkuListMoldle.h"

#import "FenLeiDDetailViewController.h"
static NSString *identifier = @"DingDanMingXiTableViewCell";
@interface GoodsMingXiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation GoodsMingXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"商品明细";
    
    
    [self addTableviewUI];
}
- (void)addTableviewUI {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-NAV_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"DingDanMingXiTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    [self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    if (_typeStr == 2) {
        TiJiaoDingDanModle *model = self.goodsArray[section];
        return model.sku_list.count;
    }else{
        return self.goodsArray.count;
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 
   
    if (_typeStr == 2) {
      return self.goodsArray.count;
    }else{
        return 1;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DingDanMingXiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (_typeStr == 2) {
        TiJiaoDingDanModle *modle = self.goodsArray[indexPath.section];
        TiJiaoDingDanSkuModle *model2 = modle.sku_list[indexPath.row];
        cell.nameLb.text = model2.name;
        
        cell.numLB.text = [NSString stringWithFormat:@"x %@",model2.num];
        [cell.lolgV sd_setImageWithURL:[NSURL URLWithString:model2.image]];
        //规格
        if (model2.value != nil && ![model2.value isKindOfClass:[NSNull class]] && model2.value.count != 0)
        {
            
            
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 0; i < model2.value.count; i++) {
                NSString *str1;
                if ([model2.value[i][@"type"] intValue] == 2) {
                    str1 = model2.value[i][@"note"];
                }else {
                    str1 = model2.value[i][@"value"];
                }
                
                [array addObject:str1];
            }
            NSArray *tempAray = [NSArray arrayWithArray:array];
            NSString *string = [tempAray componentsJoinedByString:@" "];
            cell.guigeLB.text = string;
        }
        else {
            
            cell.guigeLB.text = @"暂无更多规格";
            
        }

    }else{
        
        DingDanSkuListMoldle *model2 = self.goodsArray[indexPath.row];
        cell.nameLb.text = model2.goods_name;
        
        cell.numLB.text = [NSString stringWithFormat:@"x %@",model2.sku_num];
        [cell.lolgV sd_setImageWithURL:[NSURL URLWithString:model2.sku_image]];
        //规格
        if (model2.sku_value.length)
        {
            cell.guigeLB.text = model2.sku_value;
        }
        else {
            cell.guigeLB.text = @"暂无更多规格";
        }
    }
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    
    if (_typeStr == 2) {
        TiJiaoDingDanModle *model = self.goodsArray[section];
        UILabel *nameLable = [BasePublicCreatUI createLabelFrame:CGRectMake(20, 0, kScreenWidth-40, 30) title:[model.shop_data objectForKey:@"shop_name"] textColor:[UIColor lightGrayColor] textAlignment:NSTextAlignmentLeft font:[UIFont systemFontOfSize:14]];
        [headerView addSubview:nameLable];
    }else{
        
//        DingDanSkuListMoldle *model = self.goodsArray[0];
        UILabel *nameLable = [BasePublicCreatUI createLabelFrame:CGRectMake(20, 0, kScreenWidth-40, 30) title:@"缺" textColor:[UIColor lightGrayColor] textAlignment:NSTextAlignmentLeft font:[UIFont systemFontOfSize:14]];
        [headerView addSubview:nameLable];

    
    }
    
    
  
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    return footview;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_typeStr == 2) {
        TiJiaoDingDanModle *shopModel = self.goodsArray[indexPath.section];
        
        TiJiaoDingDanSkuModle *model = [shopModel.sku_list objectAtIndex:indexPath.row];
        
        
        FenLeiDDetailViewController *feiL = [[FenLeiDDetailViewController alloc] init];
        feiL.hidesBottomBarWhenPushed = YES;
        feiL.goodsID = model.goods_id;
        [self.navigationController pushViewController:feiL animated:YES];
    }else {
        DingDanSkuListMoldle *model2 = self.goodsArray[indexPath.row];
      
        
        
        FenLeiDDetailViewController *feiL = [[FenLeiDDetailViewController alloc] init];
        feiL.hidesBottomBarWhenPushed = YES;
        feiL.goodsID = model2.goods_id;
        
        [self.navigationController pushViewController:feiL animated:YES];
    }
    
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
